package cn.rongdao.jrkabao.ui.login;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.dao.UserDao;
import cn.rongdao.jrkabao.model.login.ModifyPasswordModel;
import cn.rongdao.jrkabao.ui.login.uicomponent.LoginInputUIComponent;
import cn.rongdao.jrkabao.util.BigDecimalUtil;
import cn.rongdao.jrkabao.util.CommonUtil;
import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function3;

/**
 * Created by jack on 2018/4/20
 */

public class ModifyPasswordActivity extends BaseAutoFinishActivity {

    public static void start(Context context) {
        FRStartActivity.start(context, ModifyPasswordActivity.class);
    }

    @BindView(R.id.amp_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.amp_old_pwd)
    LoginInputUIComponent oldPwd;
    @BindView(R.id.amp_new_pwd)
    LoginInputUIComponent newPwd;
    @BindView(R.id.amp_confirm_new_pwd)
    LoginInputUIComponent confirmNewPwd;
    @BindView(R.id.amp_tv_finish)
    TextView tvFinish;

    private CompositeDisposable compositeDisposable;

    @Override
    protected int layoutRes() {
        return R.layout.activity_modify_pwd;
    }

    @Override
    protected void initView() {
        super.initView();
        setAutoHideSoftInput(true);
        compositeDisposable = new CompositeDisposable();
        initTitleBar();
        initInput();
        initListener();
    }

    private void initListener() {
        tvFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtil.isPasswordCorrect(oldPwd.getInputContent(), newPwd.getInputContent(), confirmNewPwd.getInputContent())
                        && CommonUtil.checkPassword(newPwd.getInputContent(), confirmNewPwd.getInputContent())) {
                    modifyPwd();
                }
            }
        });
    }

    private void modifyPwd() {
        ModifyPasswordModel.getInstance()
                .addParams("old_password", BigDecimalUtil.getMD5(oldPwd.getInputContent()))
                .addParams("new_password", BigDecimalUtil.getMD5(newPwd.getInputContent()))
                .addParams("confirm_password", BigDecimalUtil.getMD5(confirmNewPwd.getInputContent()))
                .addParams("phone", UserDao.getInstance().getPhone())
                .execute(new FRBaseObserver<String>(this, "加载中，请稍候...") {
                    @Override
                    protected void onBaseNext(String data) {
                        FRToast.showToastSafe("修改密码成功");
                        finish();
                    }
                });
    }

    private void initInput() {
        oldPwd.setInputMaxLength(20);
        oldPwd.setHint("请输入原密码");
        oldPwd.initDelete();
        oldPwd.setPassword();

        newPwd.setInputMaxLength(20);
        newPwd.setHint("请输入新密码(6-20位字母或数字组成)");
        newPwd.initDelete();
        newPwd.setPassword();

        confirmNewPwd.setInputMaxLength(20);
        confirmNewPwd.setHint("请确认新密码(6-20位字母或数字组成)");
        confirmNewPwd.initDelete();
        confirmNewPwd.setPassword();

        compositeDisposable.add(Observable.combineLatest(oldPwd.getInputObservable(), newPwd.getInputObservable(), confirmNewPwd.getInputObservable(), new Function3<CharSequence, CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean apply(CharSequence oldPwdSequence, CharSequence newPwdSequence, CharSequence confirmNewPwdSequence) throws Exception {
                return !FRString.isEmpty(FRString.valueOf(oldPwdSequence), FRString.valueOf(newPwdSequence), FRString.valueOf(confirmNewPwdSequence));
            }
        }).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                tvFinish.setEnabled(aBoolean);
            }
        }));
    }

    private void initTitleBar() {
        titleBarUIComponent.initTitleBlack("修改密码");
        titleBarUIComponent.initLeftBackBlack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected boolean useBlackStatusBarOnWhite() {
        return true;
    }
}